package com.mycompany.ui.panel;

import org.springframework.beans.factory.annotation.Autowired;

import com.mycompany.config.Injector;
import com.mycompany.model.Categoria;
import com.mycompany.model.User;
import com.mycompany.service.CategoriaService;
import com.vaadin.data.fieldgroup.BeanFieldGroup;
import com.vaadin.data.fieldgroup.FieldGroup;
import com.vaadin.data.fieldgroup.FieldGroup.CommitException;
import com.vaadin.data.fieldgroup.PropertyId;
import com.vaadin.data.util.BeanItem;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.event.Action;
import com.vaadin.event.ItemClickEvent;
import com.vaadin.event.Action.Handler;
import com.vaadin.event.ItemClickEvent.ItemClickListener;
import com.vaadin.ui.Button;
import com.vaadin.ui.CheckBox;
import com.vaadin.ui.Component;
import com.vaadin.ui.FormLayout;
import com.vaadin.ui.HorizontalSplitPanel;
import com.vaadin.ui.Notification;
import com.vaadin.ui.Table;
import com.vaadin.ui.TextField;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Notification.Type;

public class CategoriaPanel extends HorizontalSplitPanel {
	
	@PropertyId("nome")
	private TextField nomeField = new TextField("Nome");
	@PropertyId("descrizione")
	private TextField descrizioneField = new TextField("Descrizione");
	@PropertyId("abilitata")
	private CheckBox checkBoxAbilitata = new CheckBox("Abilitata");
	
	@Autowired
	CategoriaService categoriaService;
	
	private Action actionDelete = new Action("Delete");

	
	private Table tabellaCategoria;
	private BeanItemContainer<Categoria> containerTabellaCategoria;
	private FieldGroup fieldGroup;
	
	public CategoriaPanel() {
		// TODO Auto-generated constructor stub
		Injector.inject(this);
		setSizeFull();
		setFirstComponent(createCategoriaTable());
		setSecondComponent(createFormCategoria());
		setSplitPosition(70, Unit.PERCENTAGE);
	}

	private Component createFormCategoria() {
		FormLayout formLayout = new FormLayout();
		formLayout.addComponent(nomeField);
		formLayout.addComponent(descrizioneField);
		formLayout.addComponent(checkBoxAbilitata);
		formLayout.addComponent(createButtonSave());
		
		return formLayout;
	}

	private Component createButtonSave() {
		// TODO Auto-generated method stub
		
		fieldGroup = new BeanFieldGroup<Categoria>(Categoria.class);
		fieldGroup.setItemDataSource(new BeanItem<Categoria>(new Categoria()));
		fieldGroup.bindMemberFields(this);
		
		Button buttonSave = new Button("Salva");
		
		buttonSave.addClickListener(new Button.ClickListener() {
			
			@Override
			public void buttonClick(ClickEvent event) {
				// TODO Auto-generated method stub
				try {
					
					fieldGroup.commit();
					fieldGroup.setBuffered(false);
					BeanItem<Categoria> categoriaToSave = (BeanItem<Categoria>) fieldGroup.getItemDataSource();
					categoriaService.save(categoriaToSave.getBean());
					containerTabellaCategoria.addBean(categoriaToSave.getBean());
					fieldGroup.setItemDataSource(new BeanItem<Categoria>(new Categoria()));
					Notification.show("Categoria Salvata");
					
					
				} catch (CommitException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					Notification.show(e.getMessage(), Type.ERROR_MESSAGE);

				}
				
			}
		});
		
		return buttonSave;
	}

	private Component createCategoriaTable() {
		
		tabellaCategoria = new Table();
		tabellaCategoria.setSizeFull();
		tabellaCategoria.setSelectable(true);
		containerTabellaCategoria = new BeanItemContainer<Categoria>(Categoria.class);
		tabellaCategoria.setContainerDataSource(containerTabellaCategoria);
		fillContainerTabella();
		
		tabellaCategoria.addItemClickListener(new ItemClickListener() {
			
			@Override
			public void itemClick(ItemClickEvent event) {
				// TODO Auto-generated method stub
				if(event.isDoubleClick())
					fieldGroup.setItemDataSource(event.getItem());
				
			}
		});
		
		tabellaCategoria.addActionHandler(new Handler() {

			@Override
			public void handleAction(Action action, Object sender, Object target) {
				// TODO Auto-generated method stub
				if(actionDelete == action){
					final Categoria categoriaToRemove = (Categoria) target;
					containerTabellaCategoria.removeItem(categoriaToRemove);
					categoriaService.deleteCategoria(categoriaToRemove);
					Notification.show("Categoria Eliminata");
				}
			}

			@Override
			public Action[] getActions(Object target, Object sender) {
				// TODO Auto-generated method stub
				return new Action [] {actionDelete};
			}
		});
		
		return tabellaCategoria;
	}

	private void fillContainerTabella() {
		// TODO Auto-generated method stub
		for(Categoria categoria : categoriaService.findAll())
			containerTabellaCategoria.addBean(categoria);
		
		 tabellaCategoria.setVisibleColumns(new Object[] {"nome", "descrizione","abilitata"});
	}

}
